/**
 * Copyright (c) qc&Bull Co.,LTD.
 * All right reserved.
 */

package cn.zxd.lis.schema;



import cn.zxd.lis.db.LDCodeDB;
import cn.zxd.util.*;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>ClassName: LDCodeSchema </p>
 * <p>Description: DB层 Schema 类文件 </p>
 * <p>Company: qc&Bull Co.,LTD </p>
 * @Database: IIEP_Data_Model
 * @author：anonymous
 * @CreateDate：2015-11-22
 */
public class LDCodeSchema implements Schema, Cloneable
{
    // @Field
    /** 编码类型 */
    private String CodeType;
    /** 编码 */
    private String Code;
    /** 编码名称 */
    private String CodeName;
    /** 编码别名 */
    private String CodeAlias;
    /** 机构代码 */
    private String ComCode = "00";
    /** 其它标志 */
    private String OtherSign;

    /** 数据库表的字段个数 */
    public static final int FIELDNUM = 6;

    /** 主键 */
    private static String[] PK;

    /** 错误信息 */
    public CErrors mErrors;

    // @Constructor
    public LDCodeSchema()
    {
        mErrors = new CErrors();
        String[] pk = new String[3];
        pk[0] = "CodeType";
        pk[1] = "Code";
        pk[2] = "ComCode";
        PK = pk;
    }

    /**
     * Schema克隆
     * @return Object
     * @throws CloneNotSupportedException
     */
    public Object clone() throws CloneNotSupportedException
    {
        LDCodeSchema cloned = (LDCodeSchema)super.clone();
        cloned.mErrors = (CErrors) mErrors.clone();
        return cloned;
    }

    // @Method
    public String[] getPK()
    {
        return PK;
    }

    public String getCodeType()
    {
        return CodeType;
    }

    public void setCodeType(String aCodeType)
    {
        CodeType = StrTool.cTrim(aCodeType);
    }

    public String getCode()
    {
        return Code;
    }

    public void setCode(String aCode)
    {
        Code = StrTool.cTrim(aCode);
    }

    public String getCodeName()
    {
        return CodeName;
    }

    public void setCodeName(String aCodeName)
    {
        CodeName = StrTool.cTrim(aCodeName);
    }

    public String getCodeAlias()
    {
        return CodeAlias;
    }

    public void setCodeAlias(String aCodeAlias)
    {
        CodeAlias = StrTool.cTrim(aCodeAlias);
    }

    public String getComCode()
    {
        return ComCode;
    }

    public void setComCode(String aComCode)
    {
        ComCode = StrTool.cTrim(aComCode);
    }

    public String getOtherSign()
    {
        return OtherSign;
    }

    public void setOtherSign(String aOtherSign)
    {
        OtherSign = StrTool.cTrim(aOtherSign);
    }


    /**
     * 使用另外一个 LDCodeSchema 对象给 Schema 赋值
     * @param: cLDCodeSchema LDCodeSchema
     */
    public void setSchema(LDCodeSchema cLDCodeSchema)
    {
        this.CodeType = cLDCodeSchema.getCodeType();
        this.Code = cLDCodeSchema.getCode();
        this.CodeName = cLDCodeSchema.getCodeName();
        this.CodeAlias = cLDCodeSchema.getCodeAlias();
        this.ComCode = cLDCodeSchema.getComCode();
        this.OtherSign = cLDCodeSchema.getOtherSign();
    }

    /**
     * 使用 ResultSet 中的第 i 行给 Schema 赋值
     * @param: rs ResultSet
     * @param: i int
     * @return: boolean
     */
    public boolean setSchema(ResultSet rs, int i)
    {
        try
        {
            if (rs.getString(1) == null)
                this.CodeType = null;
            else
                this.CodeType = rs.getString(1);
            if (rs.getString(2) == null)
                this.Code = null;
            else
                this.Code = rs.getString(2);
            if (rs.getString(3) == null)
                this.CodeName = null;
            else
                this.CodeName = rs.getString(3);
            if (rs.getString(4) == null)
                this.CodeAlias = null;
            else
                this.CodeAlias = rs.getString(4);
            if (rs.getString(5) == null)
                this.ComCode = null;
            else
                this.ComCode = rs.getString(5);
            if (rs.getString(6) == null)
                this.OtherSign = null;
            else
                this.OtherSign = rs.getString(6);
        }
        catch (SQLException sqle)
        {
            sqle.printStackTrace();
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LDCodeSchema";
            tError.functionName = "setSchema";
            tError.errorMessage = sqle.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    public LDCodeSchema getSchema()
    {
        LDCodeSchema aLDCodeSchema = new LDCodeSchema();
        aLDCodeSchema.setSchema(this);
        return aLDCodeSchema;
    }

    public LDCodeDB getDB()
    {
        LDCodeDB aDBOper = new LDCodeDB();
        aDBOper.setSchema(this);
        return aDBOper;
    }

    /**
     * 数据打包，按 XML 格式打包
     * @return: String 返回打包后字符串
     */
    public String encode()
    {
        StringBuffer strReturn = new StringBuffer(256);
        strReturn.append(StrTool.cTrim(CodeType));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(Code));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(CodeName));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(CodeAlias));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(ComCode));
        strReturn.append(SysConst.PACKAGESPILTER);
        strReturn.append(StrTool.cTrim(OtherSign));
        return strReturn.toString();
    }

    /**
     * 数据解包
     * @param: strMessage String 包含一条纪录数据的字符串
     * @return: boolean
     */
    public boolean decode(String strMessage)
    {
        try
        {
            CodeType = StrTool.getStr(strMessage, 1, SysConst.PACKAGESPILTER);
            Code = StrTool.getStr(strMessage, 2, SysConst.PACKAGESPILTER);
            CodeName = StrTool.getStr(strMessage, 3, SysConst.PACKAGESPILTER);
            CodeAlias = StrTool.getStr(strMessage, 4, SysConst.PACKAGESPILTER);
            ComCode = StrTool.getStr(strMessage, 5, SysConst.PACKAGESPILTER);
            OtherSign = StrTool.getStr(strMessage, 6, SysConst.PACKAGESPILTER);
        }
        catch (NumberFormatException ex)
        {
            // @@错误处理
            CError tError = new CError();
            tError.moduleName = "LDCodeSchema";
            tError.functionName = "decode";
            tError.errorMessage = ex.toString();
            this.mErrors.addOneError(tError);
            return false;
        }
        return true;
    }

    /**
     * 取得对应传入参数的String形式的字段值
     * @param: FCode String 希望取得的字段名
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(String FCode)
    {
        String strReturn = "";
        if (FCode.equalsIgnoreCase("CodeType"))
        {
            strReturn = StrTool.cTrim(CodeType);
        }
        if (FCode.equalsIgnoreCase("Code"))
        {
            strReturn = StrTool.cTrim(Code);
        }
        if (FCode.equalsIgnoreCase("CodeName"))
        {
            strReturn = StrTool.cTrim(CodeName);
        }
        if (FCode.equalsIgnoreCase("CodeAlias"))
        {
            strReturn = StrTool.cTrim(CodeAlias);
        }
        if (FCode.equalsIgnoreCase("ComCode"))
        {
            strReturn = StrTool.cTrim(ComCode);
        }
        if (FCode.equalsIgnoreCase("OtherSign"))
        {
            strReturn = StrTool.cTrim(OtherSign);
        }
        if (strReturn.equals(""))
        {
            strReturn = "null";
        }
        return strReturn;
    }

    /**
     * 取得Schema中指定索引值所对应的字段值
     * @param: nFieldIndex int 指定的字段索引值
     * @return: String
     * 如果没有对应的字段，返回""
     * 如果字段值为空，返回"null"
     */
    public String getV(int nFieldIndex)
    {
        String strFieldValue = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldValue = StrTool.cTrim(CodeType);
                break;
            case 1:
                strFieldValue = StrTool.cTrim(Code);
                break;
            case 2:
                strFieldValue = StrTool.cTrim(CodeName);
                break;
            case 3:
                strFieldValue = StrTool.cTrim(CodeAlias);
                break;
            case 4:
                strFieldValue = StrTool.cTrim(ComCode);
                break;
            case 5:
                strFieldValue = StrTool.cTrim(OtherSign);
                break;
            default:
                strFieldValue = "";
        }
        if (strFieldValue.equals(""))
        {
            strFieldValue = "null";
        }
        return strFieldValue;
    }

    /**
     * 设置对应传入参数的String形式的字段值
     * @param: FCode String 需要赋值的对象
     * @param: FValue String 要赋的值
     * @return: boolean
     */
    public boolean setV(String FCode, String FValue)
    {
        if (StrTool.cTrim(FCode).equals(""))
            return false;

        if (FCode.equalsIgnoreCase("CodeType"))
        {
            if (FValue != null)
            {
                CodeType = FValue.trim();
            }
            else
                CodeType = null;
        }
        if (FCode.equalsIgnoreCase("Code"))
        {
            if (FValue != null)
            {
                Code = FValue.trim();
            }
            else
                Code = null;
        }
        if (FCode.equalsIgnoreCase("CodeName"))
        {
            if (FValue != null)
            {
                CodeName = FValue.trim();
            }
            else
                CodeName = null;
        }
        if (FCode.equalsIgnoreCase("CodeAlias"))
        {
            if (FValue != null)
            {
                CodeAlias = FValue.trim();
            }
            else
                CodeAlias = null;
        }
        if (FCode.equalsIgnoreCase("ComCode"))
        {
            if (FValue != null)
            {
                ComCode = FValue.trim();
            }
            else
                ComCode = null;
        }
        if (FCode.equalsIgnoreCase("OtherSign"))
        {
            if (FValue != null)
            {
                OtherSign = FValue.trim();
            }
            else
                OtherSign = null;
        }
        return true;
    }

    public boolean equals(Object otherObject)
    {
        if (this == otherObject)
            return true;
        if (otherObject == null)
            return false;
        if (getClass() != otherObject.getClass())
            return false;
        LDCodeSchema other = (LDCodeSchema) otherObject;
        return
                (CodeType == null ? other.getCodeType() == null : CodeType.equals(other.getCodeType()))
                        && (Code == null ? other.getCode() == null : Code.equals(other.getCode()))
                        && (CodeName == null ? other.getCodeName() == null : CodeName.equals(other.getCodeName()))
                        && (CodeAlias == null ? other.getCodeAlias() == null : CodeAlias.equals(other.getCodeAlias()))
                        && (ComCode == null ? other.getComCode() == null : ComCode.equals(other.getComCode()))
                        && (OtherSign == null ? other.getOtherSign() == null : OtherSign.equals(other.getOtherSign()));
    }

    /**
     * 取得Schema拥有字段的数量
     * @return: int
     */
    public int getFieldCount()
    {
        return FIELDNUM;
    }

    /**
     * 取得Schema中指定字段名所对应的索引值
     * 如果没有对应的字段，返回-1
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldIndex(String strFieldName)
    {
        if (strFieldName.equals("CodeType"))
        {
            return 0;
        }
        if (strFieldName.equals("Code"))
        {
            return 1;
        }
        if (strFieldName.equals("CodeName"))
        {
            return 2;
        }
        if (strFieldName.equals("CodeAlias"))
        {
            return 3;
        }
        if (strFieldName.equals("ComCode"))
        {
            return 4;
        }
        if (strFieldName.equals("OtherSign"))
        {
            return 5;
        }
        return -1;
    }

    /**
     * 取得Schema中指定索引值所对应的字段名
     * 如果没有对应的字段，返回""
     * @param: nFieldIndex int
     * @return: String
     */
    public String getFieldName(int nFieldIndex)
    {
        String strFieldName = "";
        switch (nFieldIndex)
        {
            case 0:
                strFieldName = "CodeType";
                break;
            case 1:
                strFieldName = "Code";
                break;
            case 2:
                strFieldName = "CodeName";
                break;
            case 3:
                strFieldName = "CodeAlias";
                break;
            case 4:
                strFieldName = "ComCode";
                break;
            case 5:
                strFieldName = "OtherSign";
                break;
            default:
                strFieldName = "";
        }
        return strFieldName;
    }

    /**
     * 取得Schema中指定字段名所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: strFieldName String
     * @return: int
     */
    public int getFieldType(String strFieldName)
    {
        if (strFieldName.equals("CodeType"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("Code"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("CodeName"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("CodeAlias"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("ComCode"))
        {
            return Schema.TYPE_STRING;
        }
        if (strFieldName.equals("OtherSign"))
        {
            return Schema.TYPE_STRING;
        }
        return Schema.TYPE_NOFOUND;
    }

    /**
     * 取得Schema中指定索引值所对应的字段类型
     * 如果没有对应的字段，返回Schema.TYPE_NOFOUND
     * @param: nFieldIndex int
     * @return: int
     */
    public int getFieldType(int nFieldIndex)
    {
        int nFieldType = Schema.TYPE_NOFOUND;
        switch (nFieldIndex)
        {
            case 0:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 1:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 2:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 3:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 4:
                nFieldType = Schema.TYPE_STRING;
                break;
            case 5:
                nFieldType = Schema.TYPE_STRING;
                break;
            default:
                nFieldType = Schema.TYPE_NOFOUND;
        }
        return nFieldType;
    }
}
